/*
 * Experiment.java
 * 
 * Created on Mar 31, 2009, 1:15:13 AM
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package portfolio.dbHost.project.attributes.experiments;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import portfolio.dbHost.project.Project;
import portfolio.dbHost.project.attributes.StockExperiments;
import portfolio.mainScreen;
import portfolio.stock.ConfigureSVMPanel;
import portfolio.stock.SelectVariables;
import portfolio.stock.analize.AnalizeArguments;

/**
 *
 * @author ACA
 */
/////////////////////////////////////////////////////////////////////////////////////
public class Experiment implements MouseListener
{
    private mainScreen       fatherFrame      = null;
    private StockExperiments stockExperiments = null;
    private String           sExperimentName  = null;
    
    static final public String COLUMN_EXPERIMNETNAME = "name";
    
    static final public String ACTION_DROP_EXPERIMENT  = "Drop";
    static final public String ACTION_LOAD_EXPERIMENT  = "Load";
    
//    private Vector vStockExperiments = null;
    
    /**
     * 
     * @param e 
     */
    public void mouseClicked(MouseEvent e) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mousePressed(MouseEvent e) {
        try {
            Object o = e.getSource();
            JMenuItem jTempMenuItem = (javax.swing.JMenuItem) o;
            String sCommand = jTempMenuItem.getActionCommand();

            if (ACTION_LOAD_EXPERIMENT.equals(sCommand)) {
                ResultSet resultSet= getColumnQuery (StockExperiments.COLUMN_VARIABLES, sExperimentName);
                
                ObjectInputStream vectorInputStream = this.getBlobOBject (resultSet, StockExperiments.COLUMN_VARIABLES);
                Vector vSelectList = (Vector) vectorInputStream.readObject();

                resultSet= getColumnQuery (StockExperiments.COLUMN_SVMCONFIG, sExperimentName);
                
                ObjectInputStream analizeInputStream = this.getBlobOBject (resultSet, StockExperiments.COLUMN_SVMCONFIG);
                AnalizeArguments analizeArguments = (AnalizeArguments) analizeInputStream.readObject();
                
                String sStockName = this.getStockName (sExperimentName);
                
                fatherFrame.repaintStockTabedPanels();
                SelectVariables    variables = new SelectVariables (fatherFrame, sStockName, vSelectList);
                ConfigureSVMPanel ConfigAttributes = new ConfigureSVMPanel (fatherFrame, sStockName, variables, analizeArguments);
                
                fatherFrame.getStockItems().setSelectVariables (variables);
                fatherFrame.getSelectVariablesPanel().revalidate();
                
                fatherFrame.displayMessage("portfolio.dbHost.project.attributes.experiments.Experiment.mousePressed 97", "Project experiment: " + sExperimentName + " loaded.");
            } else if (ACTION_DROP_EXPERIMENT.equals(sCommand)) {
//               Table stockExpTable = stockExperiments.getTable();
                stockExperiments.getTable().deleteRows("name = '" + sExperimentName + "'");
                fatherFrame.displayMessage("portfolio.dbHost.project.attributes.experiments.Experiment.mousePressed 100", "Project experiment: " + sExperimentName + " droped.");

                Project project = fatherFrame.getDBLoggedHost().getProject();
                project.updateProjectTree();

                fatherFrame.repaintStockTabedPanels (); //NOT original
            }
        } catch (Exception ex) {
            fatherFrame.displayMessage("portfolio.dbHost.project.attributes.experiments.Experiment.mousePressed 104", ex.getMessage ());
        }
   }

    public void mouseReleased(MouseEvent e) {
 //       throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent e) {
 //       throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
//        throw new UnsupportedOperationException("Not supported yet.");
//        e.equals(MouseEvent.BUTTON1);
    }
    /** Creates a new instance of ExcludeTables 
     * @param projectParam 
     * @throws java.lang.Exception 
     */
    public Experiment (StockExperiments experimentsParam, String sExperimentNameParam) throws Exception
    {
        stockExperiments = experimentsParam;
        fatherFrame      = stockExperiments.getTable().getDBase().getDBHost().getMainScreen();
        sExperimentName  = sExperimentNameParam;
    }
    /**
     *
     * @return 
     * @throws java.lang.Exception 
     */
    public DefaultMutableTreeNode processExperimentHierarchy ( ) throws Exception 
    {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode (sExperimentName);         
        return node;
    }
    /**
     *
     * @param dbTreeParam 
     * @param iXPos 
     * @param iYPos 
     */
     public void getPopupOptions (JTree dbTreeParam, int iXPos, int iYPos)
    {
        JTree dbaseTree = dbTreeParam;
        
        JPopupMenu treeOptionsMenu = new JPopupMenu ("Options");
        dbaseTree.add(treeOptionsMenu);
        
        treeOptionsMenu.addMouseListener(this);
        
        JMenuItem jItem1 = new JMenuItem (ACTION_LOAD_EXPERIMENT);
        jItem1.addMouseListener (this);
        treeOptionsMenu.add (jItem1); 
        JMenuItem jItem2 = new JMenuItem (ACTION_DROP_EXPERIMENT);
        jItem2.addMouseListener (this);
        treeOptionsMenu.add (jItem2); 
        
        treeOptionsMenu.setLocation (iXPos, iYPos);
        treeOptionsMenu.show (treeOptionsMenu, iXPos, iYPos);
        
        return;
    } /* end method getPopupOptions */      
    /*
     * 
     */
    public Object getTreeObject (Object [] oPathParam)
    {
        if (oPathParam == null) {
            return null;
        }
        if (oPathParam.length <3) {
            return null;
        }
        
        return this;
    } /* end method getTreeObject */
    /**
     * 
     */
    private ObjectInputStream getBlobOBject (ResultSet resultSet, String sBlobColumn)
    throws SQLException, Exception 
    {
        
        Blob blob =  resultSet.getBlob(sBlobColumn);

        Long LBlobLen = blob.length();
        if (LBlobLen == null) {
            throw new Exception ("Error: null BlobLen.");
        }

        int iBlobLen = LBlobLen.intValue();
        if (iBlobLen <= 0) {
            throw new Exception ("Error: Blob value <= 0."); //report an error
        }

        byte [] b = new byte [iBlobLen];
        b = blob.getBytes(1, iBlobLen);

        ByteArrayInputStream bInputStream = new ByteArrayInputStream (b);
        return new ObjectInputStream (bInputStream);
    }
    /**
     * 
     */
    private String getStockName (String sExperimentNameParam)
    {

        int iPointIndex = sExperimentNameParam.indexOf(".");
        String sStockName = null;
        if (iPointIndex > 0)
            sStockName = sExperimentNameParam.substring(0, iPointIndex);
        else
            sStockName = sExperimentNameParam;
        
        return sStockName;
    }
    /**
     * 
     */
    private ResultSet getColumnQuery (String sColumn, String sExperimentNameParam) throws Exception 
    {
        ResultSet resultSet = stockExperiments.getTable().getQuery(sColumn, 
                                                                   StockExperiments.COLUMN_EXPERIMNETNAME + " = '" + sExperimentNameParam + "'",
                                                                   sColumn);
        resultSet.last();
        int iResultSetLen = resultSet.getRow();
        if (iResultSetLen != 1) {
            throw new Exception ("Error: unexpected resultSet length.");
        }
        resultSet.first();
        return resultSet;
    }
}
